<?php
 /**
  * @datetime 15-1-14
  * CssCompressor.php
  */
/*
$css = new \ulue\libs\compress\driver\CssCompressor;

$c = $css->execute( [
    STATIC_PATH.'/packages/debug/css/debug.css',
    STATIC_PATH.'/css/my.custom.css',
    ])->save(STATIC_PATH.'/publish/css/my.css');
 */
namespace ulue\libs\compress\driver;

use ulue\libs\helpers\StringHelper,
    ulue\libs\compress\AbstractCompress;

class CssCompressor extends AbstractCompress
{

    public function __construct()
    {
        parent::__construct();
        // $this->checkExtensionLoaded();
    }

    public function suffix()
    {
        return 'css';
    }

    /**
     * [execute 执行压缩 css 文件]
     * @param  string | array $files
     * @param bool $return
     * @throws \DMissArgumentException
     * @throws \DNotFoundException
     * @example
     *     string: 'xx.css, yy.css'
     *     array: ['xx.css' , 'yy.css']
     * @return $this|string [type]        [description]
     */
    public function execute($files, $return=false)
    {
        $files = $this->checkFileExists($files,$this->getSuffix());

        ob_start();
        //包含你的全部css文档
        foreach ($files as $file)
        {
            include($file);
        }

        //得到buffer中的内容
        $this->output = ob_get_clean();

        $this->clearChar();

        if ($return) {
            return $this->output;
        }

        return $this;
    }

    public function pregRules()
    {
        return [
            'pattern' => [
                '!/\*[^*]*\*+([^/][^*]*\*+)*/!', //去除文件中的注释
                "/\s\s+/i" // 多个空格 换成一个
            ],

            'replace' => ['',' '],
        ];
    }

}
